/*
 * @Author: hua
 * @Date: 2020-07-17 17:03:32
 * @description: 
 * @LastEditors: hua
 * @LastEditTime: 2020-07-23 10:53:40
 */
import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import store from '../store'
import i18n from '../lang'
// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 15000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  if (store.getters.token) {
    config.headers['X-Token'] = store.getters.token// 让每个请求携带自定义token 请根据实际情况自行修改
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    /**
    * code为非20000是抛错 可结合自己业务进行修改
    */
    const res = response.data
    if (res.code !== "000000") {
      let errorMsg = parseCode(res);
      Message({
        message: errorMsg,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error)// for debug
    /* 网络超时处理 */
    if (error == 'Error: timeout of 15000ms exceeded') {
      Message({
        message: i18n.t('timeout'),
        type: 'error',
        duration: 5 * 1000
      })
    }
    /* 网络无响应处理 */
    if (typeof error.response === 'undefined') {
      if (error == 'Error: Network Error') {
        Message({
          message: i18n.t('networkError'),
          type: 'error',
          duration: 5 * 1000
        })
      }
    }
    return Promise.reject(error)
  }
)

function parseCode(res) {
  let msg = ""
  if (res.code === '000001') {
    msg = i18n.t('FAILED');
  }
  if (res.code === '000002') {
    msg = i18n.t('PARAMETER_CHECK_FAILED');
  }
  if (res.code === '000002') {
    msg = i18n.t('PARAMETER_CHECK_FAILED');
  }
  if (res.code === '000003') {
    msg = i18n.t('SYSTEM_EXCEPTION');
  }
  if (res.code === '000004') {
    msg = i18n.t('TOKEN_ERROR');
  }
  if (res.code === '000005') {
    msg = i18n.t('INSUFFICIENT_PERMISSIONS');
  }
  if (res.code === '000006') {
    msg = i18n.t('LOGIN_FAILURE');
  }
  if (res.code === '110001') {
    msg = i18n.t('USER_DOES_NOT_EXIST');
  }
  if (res.code === '110002') {
    msg = i18n.t('ORIGINAL_PASSWORD_WRONG');
  }
  if (res.code === '110003') {
    msg = i18n.t('LOGIN_PASSWORD_WRONG');
  }
  if (res.code === '110004') {
    msg = i18n.t('NO_BINDING_INFO');
  }
  if (res.code === '100001') {
    msg = i18n.t('DEVICE_TYPE_DOES_NOT_EXIST');
  }
  if (res.code === '100002') {
    msg = i18n.t('DEVICE_DOES_NOT_EXIST');
  }
  if (res.code === '100003') {
    msg = i18n.t('DEVICE_TAG_DOES_NOT_EXIST');
  }
  if (res.code === '100004') {
    msg = i18n.t('DEVICE_FUNCTION_DOES_NOT_EXIST');
  }
  if (res.code === '100005') {
    msg = i18n.t('PARAMETER_OF_SEND_COMMAND_IS_WRONG');
  }
  if (res.code === '100006') {
    msg = i18n.t('CAN_NOT_FIND_MQ_CONSUMER');
  }
  if (res.code === '100007') {
    msg = i18n.t('DEVICE_FUNCTION_EXISTED');
  }
  if (res.code === '100008') {
    msg = i18n.t('DEVICE_MESSAGE_EXISTED');
  }
  if (res.code === '100009') {
    msg = i18n.t('DEVICE_GROUP_DOES_NOT_EXIST');
  }
  if (res.code === '100010') {
    msg = i18n.t('DEVICE_GROUP_DOES_NOT_EXIST');
  }
  if (res.code === '110005') {
    msg = i18n.t('QR_CODE_FORMAT_ERROR');
  }
  if (res.code === '110006') {
    msg = i18n.t('BINDING_INFO_EXISTS');
  }
  return msg;
}
export default service
